package com.fitbet.admin.web.controller.bonus;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fitbet.admin.entity.search.bonus.UserWithdrawalSearch;
import com.fitbet.admin.service.bonus.WithdrawalAuditingsService;
import com.fitbet.entity.user.UserWithdrawal;
/**
 * 提现待审核
 * @author sjh
 *
 */
@Controller
@RequestMapping("/withdrawal-auditings")
@MenuSetting("menu-bonus")
public class WithdrawalAuditingsController {
    
    private static final Logger logger = LoggerFactory.getLogger(WithdrawalAuditingsController.class);

    @Autowired
    private WithdrawalAuditingsService userBetService;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-withdrawal-auditings-list")
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("bonus/withdrawal-auditings/withdrawal-auditings-list");
        
        int totals = userBetService.count();
        
        if (totals >0 ) {
            
            List<UserWithdrawal> userWithdrawals = userBetService.list(page.with(totals));
            modelAndView.addObject("userWithdrawals", userWithdrawals);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
    }
    
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "bonus/withdrawal-auditings/withdrawal-auditings-search";
    }
    
    /**
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-withdrawal-auditings-list")
    public ModelAndView onSearch(UserWithdrawalSearch search, Page page, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("bonus/withdrawal-auditings/withdrawal-auditings-list");
        
        int totals = userBetService.countSearch(UserWithdrawal.class, search.getPreparedFilter(false));
        if (totals > 0) {
            
            List<UserWithdrawal> userWithdrawals = userBetService.listSearch(search, page.with(totals));
            modelAndView.addObject("userWithdrawals", userWithdrawals);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter","page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 标记已支付面板
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/pay", method = RequestMethod.GET)
    @MenuSetting("submenu-withdrawal-auditings-list")
    public ModelAndView pay(@PathVariable Long id) {
        
        ModelAndView modelAndView = new ModelAndView("bonus/withdrawal-auditings/withdrawal-auditings-pay");
        modelAndView.addObject("id", id);
       
        return modelAndView;
        }
    
    /**
     * 标记已支付
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/pay", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onPay(@PathVariable Long id) {
        
        HttpResult httpResult = new HttpResult();
        try {
            
            UserWithdrawal userWithdrawal = userBetService.load(UserWithdrawal.class, id);
            
            userBetService.pay(userWithdrawal);
            
            httpResult.success().message("标记支付成功");
            
        } catch (BusinessException e) {
            
            logger.error("#onPay() occurs error", e);
            
            httpResult.fail(e.getCode()).message(e.getMessage());
            
        } catch (Exception e) {
           
            logger.error("#onPay() occurs error", e);
           
            httpResult.fail("500").message("标记支付失败");
        }
        return httpResult;
        
    }
    
}
